//货币格式化
import accounting from 'accounting';
//时间日期格式化
import moment from 'moment';

// 全局方法

export default {
    install(Vue, options) {
        //判断是否为空，0或null
        Vue.prototype.isNull0 = function (data, txt) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                if(txt == undefined || txt == 0 || txt == null || txt == ''){
                    val = data;
                }else{
                    val = data + txt;
                }
            }
            return val;
        };

        //判断图片是否为空或null，使用替代图
        Vue.prototype.isNullImg = function (src) {
            let val = '';
            if (src == null || src == '') {
                val = '/static/img/houseList-noData.jpg';
            } else {
                val = src;
            }
            return val;
        };

        //判断顾问头像图片是否为空或null，使用替代图
        Vue.prototype.consultantPhotoIsNullImg = function (src) {
            let val = '';
            if (src == null || src == '') {
                val = '/static/img/upload-Head-notHead.jpg';
            } else {
                val = src;
            }
            return val;
        };

        //判断美元价钱信息是否为0或null
        Vue.prototype.isNull0_USD = function (data, txt) {
            let val = '';
            if (data == null || data == 0 || data == '$0' || data == '$-') {
                val = '-';
            } else {
                if (txt == undefined || txt == '') {
                    //判断是否整数，是则返回true，否则返回false
                    if(Math.round(data) === data){
                        val = accounting.formatMoney(data, "$ ", 0);
                    }else{
                        val = accounting.formatMoney(data, "$ ", 1);
                    }
                } else {
                    //判断是否整数，是则返回true，否则返回false
                    if(Math.round(data) === data){
                        val = accounting.formatMoney(data, "$ ", 0) + txt;
                    }else{
                        val = accounting.formatMoney(data, "$ ", 1) + txt;
                    }
                }
            }
            return val;
        };

        //判断泰铢价钱信息是否为0或null
        Vue.prototype.isNull0_THB = function (data, txt) {
            let val = '';
            if (data == null || data == 0 || data == '฿0' || data == '฿-') {
                val = '-';
            } else {
                if (txt == undefined || txt == '') {
                    //判断是否整数，是则返回true，否则返回false
                    if(Math.round(data) === data){
                        val = accounting.formatMoney(data, "฿ ", 0);
                    }else{
                        val = accounting.formatMoney(data, "฿ ", 1);
                    }
                } else {
                    //判断是否整数，是则返回true，否则返回false
                    if(Math.round(data) === data){
                        val = accounting.formatMoney(data, "฿ ", 0) + txt;
                    }else{
                        val = accounting.formatMoney(data, "฿ ", 1) + txt;
                    }
                }
            }
            return val;
        };

        //判断欧元价钱信息是否为0或null
        Vue.prototype.isNull0_EUR = function (data, txt) {
            let val = '';
            if (data == null || data == 0 || data == '€0' || data == '€-') {
                val = '-';
            } else {
                if (txt == undefined || txt == '') {
                    //判断是否整数，是则返回true，否则返回false
                    if(Math.round(data) === data){
                        val = accounting.formatMoney(data, "€ ", 0);
                    }else{
                        val = accounting.formatMoney(data, "€ ", 1);
                    }
                } else {
                    //判断是否整数，是则返回true，否则返回false
                    if(Math.round(data) === data){
                        val = accounting.formatMoney(data, "€ ", 0) + txt;
                    }else{
                        val = accounting.formatMoney(data, "€ ", 1) + txt;
                    }
                }
            }
            return val;
        };

        //判断日元价钱信息是否为0或null
        Vue.prototype.isNull0_JPY = function (data, txt) {
            let val = '';
            if (data == null || data == 0 || data == '￥0' || data == '￥-') {
                val = '-';
            } else {
                if (txt == undefined || txt == '') {
                    //判断是否整数，是则返回true，否则返回false
                    if(Math.round(data) === data){
                        val = accounting.formatMoney(data, "JPY ￥ ", 0);
                    }else{
                        val = accounting.formatMoney(data, "JPY ￥ ", 1);
                    }
                } else {
                    //判断是否整数，是则返回true，否则返回false
                    if(Math.round(data) === data){
                        val = accounting.formatMoney(data, "JPY ￥ ", 0) + txt;
                    }else{
                        val = accounting.formatMoney(data, "JPY ￥ ", 1) + txt;
                    }
                }
            }
            return val;
        };

        //判断人民币价钱信息是否为0或null
        Vue.prototype.isNull0_RMB = function (data, txt, currency) {
            //取出美元的汇率数据
            let USD = {};
            if(this.$store.getters.get_exchangeRate){
                this.$store.getters.get_exchangeRate.forEach((i) => {
                    if(i.currencyName == '美元'){
                        USD = i.exchangeRate;
                    }
                });
            }

            //取出泰铢的汇率数据
            let THB = {};
            if(this.$store.getters.get_exchangeRate){
                this.$store.getters.get_exchangeRate.forEach((i) => {
                    if(i.currencyName == '泰国铢'){
                        THB = i.exchangeRate;
                    }
                });
            }

            //取出欧元的汇率数据
            let EUR = {};
            if(this.$store.getters.get_exchangeRate){
                this.$store.getters.get_exchangeRate.forEach((i) => {
                    if(i.currencyName == '欧元'){
                        EUR = i.exchangeRate;
                    }
                });
            }

            //取出日元的汇率数据
            let JPY = {};
            if(this.$store.getters.get_exchangeRate){
                this.$store.getters.get_exchangeRate.forEach((i) => {
                    if(i.currencyName == '日元'){
                        JPY = i.exchangeRate;
                    }
                });
            }

            let val = '';
            if (data == null || data == 0 || data == '') {
                val = '-';
            } else {
                //判断货币单位是否美元、泰铢、欧元、日元
                if(currency == 'USD'){
                    if (txt == undefined || txt == '') {
                        val = accounting.formatMoney(parseInt(data * USD), "约CNY ¥ ", 0);
                    } else {
                        val = accounting.formatMoney(parseInt(data * USD), "约CNY ¥ ", 0) + txt;
                    }
                }else if(currency == 'THB'){
                    if (txt == undefined || txt == '') {
                        val = accounting.formatMoney(parseInt(data * THB), "约CNY ¥ ", 0);
                    } else {
                        val = accounting.formatMoney(parseInt(data * THB), "约CNY ¥ ", 0) + txt;
                    }
                }else if(currency == 'EUR'){
                    if (txt == undefined || txt == '') {
                        val = accounting.formatMoney(parseInt(data * EUR), "约CNY ¥ ", 0);
                    } else {
                        val = accounting.formatMoney(parseInt(data * EUR), "约CNY ¥ ", 0) + txt;
                    }
                }else if(currency == 'JPY'){
                    if (txt == undefined || txt == '') {
                        val = accounting.formatMoney(parseInt(data * JPY), "约CNY ¥ ", 0);
                    } else {
                        val = accounting.formatMoney(parseInt(data * JPY), "约CNY ¥ ", 0) + txt;
                    }
                }
            }
            return val;
        };

        //判断平方英尺是否为0或null
        Vue.prototype.isNull0_sqft = function (data, txt) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                val = data +'平方英尺';
            }
            return val;
        };

        //判断平方米是否为0或null
        Vue.prototype.isNull0_sqmt = function (data, txt) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                val ='约'+ parseInt(data * 0.0929) +'平米';
            }
            return val;
        };

        //moment时间格式化
        Vue.prototype.momentFormat = function (data, formatString) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                val = moment(data).format(formatString);
            }
            return val;
        };

        //性别判断
        Vue.prototype.sex_FN = function (data) {
            let val = '';
            if (data == 1) {
                val = '先生';
            } else if (data == 0) {
                val = '女士';
            } else {
                val = '-';
            }
            return val;
        };

        //手机号正则验证
        const phoneReg = /^(1)[3|4|5|6|7|8|9](\d){9}$/;
        Vue.prototype.phone_validation = function (rule, value, callback) {
            if (phoneReg.test(value)) {
                callback()
            } else {
                callback(new Error('手机号格式为11位数字，不能含有空格！'))
            }
        };

        //邮箱正则验证
        const emailReg = /[\w!#$%&'*+/=?^_`{|}~-]+(?:\.[\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\w](?:[\w-]*[\w])?\.)+[\w](?:[\w-]*[\w])?/;
        Vue.prototype.email_validation = function (rule, value, callback) {
            if (emailReg.test(value)) {
                callback()
            } else {
                callback(new Error('邮箱格式不能含有空格、不能为空！'))
            }
        };

        //验证码6位数验证
        Vue.prototype.code_validation = function (rule, value, callback) {
            if (value != undefined && value.toString().length == 6) {
                callback()
            } else {
                callback(new Error('验证码格式为6位数字，不能含有空格、不能为空！'))
            }
        };

        //判断此房源是否被当前登录用户收藏过
        Vue.prototype.isCollectedHouse = function (id, collectionHouseIdItem) {
            let iParams = {};
            if (collectionHouseIdItem.includes(parseInt(id))) {
                iParams = {
                    class: 'iconfont icon-xin1',
                    tooltip: '取消收藏',
                    innerHTML: '<i class="iconfont icon-xin1"></i> 取消收藏',
                };
            } else {
                iParams = {
                    class: 'iconfont icon-icon-test',
                    tooltip: '收藏房源',
                    innerHTML: '<i class="iconfont icon-icon-test"></i> 收藏房源',
                };
            }
            return iParams;
        };

        //弹出美恰客服
        Vue.prototype.callMEIQIA = function () {
            _MEIQIA('showPanel');
        };

        //判断最大面积的值是否为空
        Vue.prototype.isMinMaxArea = function (data) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = 'm²起';
            } else {
                val = '-' + data + 'm²';
            }
            return val;
        };

        //判断最大价钱的值是否为空
        Vue.prototype.isMinMaxPrice = function (data) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '起';
            } else {
                //判断是否整数，是则返回true，否则返回false
                if(Math.round(data) === data){
                    val = '- '+ accounting.formatMoney(data, "", 0) + '万';
                }else{
                    val = '- '+ accounting.formatMoney(data, "", 1) + '万';
                }
            }
            return val;
        };

        //判断换算成人民币后，最大价钱的值是否为空
        Vue.prototype.isMinMaxRMBPrice = function (data, currency) {
            //取出美元的汇率数据
            let USD = {};
            if(this.$store.getters.get_exchangeRate){
                this.$store.getters.get_exchangeRate.forEach((i) => {
                    if(i.currencyName == '美元'){
                        USD = i.exchangeRate;
                    }
                });
            }

            //取出泰铢的汇率数据
            let THB = {};
            if(this.$store.getters.get_exchangeRate){
                this.$store.getters.get_exchangeRate.forEach((i) => {
                    if(i.currencyName == '泰国铢'){
                        THB = i.exchangeRate;
                    }
                });
            }

            //取出欧元的汇率数据
            let EUR = {};
            if(this.$store.getters.get_exchangeRate){
                this.$store.getters.get_exchangeRate.forEach((i) => {
                    if(i.currencyName == '欧元'){
                        EUR = i.exchangeRate;
                    }
                });
            }

            //取出日元的汇率数据
            let JPY = {};
            if(this.$store.getters.get_exchangeRate){
                this.$store.getters.get_exchangeRate.forEach((i) => {
                    if(i.currencyName == '日元'){
                        JPY = i.exchangeRate;
                    }
                });
            }

            let val = '';
            if (data == null || data == 0 || data == '') {
                val = '起';
            } else {
                //判断货币单位是否美元、泰铢、欧元
                if(currency == 'USD'){
                    val = '- '+ accounting.formatMoney(parseInt(data * USD), "", 0) + '万';
                }else if(currency == 'THB'){
                    val = '- '+ accounting.formatMoney(parseInt(data * THB), "", 0) + '万';
                }else if(currency == 'EUR'){
                    val = '- '+ accounting.formatMoney(parseInt(data * EUR), "", 0) + '万';
                }else if(currency == 'JPY'){
                    val = '- '+ accounting.formatMoney(parseInt(data * JPY), "", 0) + '万';
                }
            }
            return val;
        };


        //判断传给全局方法换算人民币使用——要换算的货币单位
        Vue.prototype.isCurrency = function (countryName) {
            let val = '';
            //判断货币单位是否美元、泰铢、欧元
            if(countryName == '美国'){
                val = 'USD';
            }else if(countryName == '泰国'){
                val = 'THB';
            }else if(countryName == '希腊'){
                val = 'EUR';
            }else if(countryName == '日本'){
                val = 'JPY';
            }
            return val;
        };

        //判断货币单位，展示对应货币符号
        Vue.prototype.isCurrencySymbol = function (countryName) {
            let val = '';
            //判断货币单位是否美元、泰铢、欧元
            if(countryName == '美国'){
                val = '$';
            }else if(countryName == '泰国'){
                val = '฿';
            }else if(countryName == '希腊'){
                val = '€';
            }else if(countryName == '日本'){
                val = 'JPY ￥';
            }
            return val;
        };

        //只显示每个房源的房源类型标签的第一个
        Vue.prototype.isSplit_houseType = function (data) {
            let val = '';
            if (data == 0 || data == null || data == '') {
                val = '-';
            } else {
                val = data.split(',')[0];
            }
            return val;
        };

        //localStorage过期控制代码——新增local
        Vue.prototype.setLocalStorage = function (key, value) {
            let curTime = new Date().getTime();
            localStorage.setItem(key, JSON.stringify({ data: value, curTime: curTime }));
        };
        //localStorage过期控制代码——获取local并判断是否过期
        Vue.prototype.getLocalStorage = function (key, expired) {
            let dataStr = localStorage.getItem(key);
            if(dataStr){
                let dataObj = JSON.parse(dataStr);
                if (new Date().getTime() - dataObj.curTime > expired) {
                    return false;
                }else{
                    return dataObj.data;
                }
            }else{
                return false;
            }
        };
    }
}
